﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using System.Windows.Controls;
using System.Windows;
using MongoDB.Bson;
using WpfMongoDB.Model;
using MongoDB.Driver.Builders;
using System.Data;

namespace WpfMongoDB
{
    public class BindTreeData
    {
        MainWindow mw;
        public BindTreeData(MainWindow _mw)
        {
            mw = _mw; 
        }
        /// <summary>
        /// 绑定树节点
        /// </summary>
        /// <param name="key"></param>
        /// <param name="server"></param>
        /// <returns></returns>
        public TreeNodeModel Bind(string key, MongoServer server)
        {
            var dbs = server.GetDatabaseNames();
            TreeNodeModel titem = new TreeNodeModel();
            titem.Name = key;
            titem.Icon = "/images/(00,48).png";
            foreach (var dbname in dbs)
            {
                TreeNodeModel database = new TreeNodeModel();
                database.Name = dbname;
                database.Icon = "/images/(42,48).png";
                database.Type = TreeNodeType.DataBase;
                var db = server.GetDatabase(dbname);

                TreeNodeModel collection = new TreeNodeModel();
                collection.Name = "Collections";
                collection.Icon = "/images/(05,08).png";
                database.ChildItem.Add(collection);

                TreeNodeModel user = new TreeNodeModel();
                user.Name = "User";
                user.Icon = "/images/(07,42).png";
                database.ChildItem.Add(user);

                TreeNodeModel JavaScript = new TreeNodeModel();
                JavaScript.Name = "JavaScript";
                JavaScript.Icon = "/images/(05,08).png";
                database.ChildItem.Add(JavaScript);

                TreeNodeModel gfs = new TreeNodeModel();
                gfs.Name = "GFS";
                gfs.Icon = "/images/(05,08).png";
                gfs.Type = TreeNodeType.GFS;
                var collectionNames = db.GetCollectionNames();
                foreach (var collectionName in collectionNames)
                {
                    if (collectionName=="system.indexes")
                    {
                        continue;
                    }
                    TreeNodeModel table = new TreeNodeModel();
                    table.Name = collectionName;
                    table.Icon = "/images/(31,20).png";
                    table.Type = TreeNodeType.Collection;
                    table.Collection = db.GetCollection(collectionName);
                    collection.ChildItem.Add(table);
                    //索引
                    TreeNodeModel indexfolder = new TreeNodeModel();
                    indexfolder.Name = "indexs";
                    indexfolder.Icon = "/images/(05,08).png";
                    table.ChildItem.Add(indexfolder);
                    var cbean = db.GetCollection(collectionName);
                    var indexs = cbean.GetIndexes();
                    foreach (var index in indexs)
                    {
                        TreeNodeModel tnindex = new TreeNodeModel();
                        tnindex.Name = index.Name;
                        tnindex.Icon = "/images/(07,40).png";
                        tnindex.Type = TreeNodeType.Index;
                        indexfolder.ChildItem.Add(tnindex);
                    }
                }
                titem.ChildItem.Add(database);
            }
            return titem;
        }

        /// <summary>
        /// 表菜单
        /// </summary>
        /// <returns></returns>
        public ContextMenu BindCollectionMenu(TreeNodeModel bean)
        {
            ContextMenu cmenu = new ContextMenu();
            MenuItem showT200 = new MenuItem();
            showT200.Header = "显示100条";
            showT200.Tag = bean.Collection;
            showT200.Click += new RoutedEventHandler(Show_Top_200);
            cmenu.Items.Add(showT200);

            MenuItem showT1k = new MenuItem();
            showT1k.Header = "显示1000条";
            showT1k.Tag = bean.Name;
            showT1k.Click += new RoutedEventHandler(Show_Top_1k);
            cmenu.Items.Add(showT1k);

            MenuItem find = new MenuItem();
            find.Header = "查询";
            find.Tag = bean.Name;
            find.Click += new RoutedEventHandler(Find_Click);
            cmenu.Items.Add(find);

            MenuItem update = new MenuItem();
            update.Header = "更新";
            update.Tag = bean.Name;
            update.Click += new RoutedEventHandler(Update_Click);
            cmenu.Items.Add(update);

            MenuItem remove = new MenuItem();
            remove.Header = "删除";
            remove.Tag = bean.Name;
            remove.Click += new RoutedEventHandler(Remove_Click);
            cmenu.Items.Add(remove);

            MenuItem insert = new MenuItem();
            insert.Header = "插入Document";
            insert.Tag = bean.Name;
            insert.Click += new RoutedEventHandler(Insert_Click);
            cmenu.Items.Add(insert);

            MenuItem index = new MenuItem();
            index.Header = "添加索引";
            index.Tag = bean.Name;
            index.Click += new RoutedEventHandler(Index_Click);
            cmenu.Items.Add(index);

            MenuItem compact = new MenuItem();
            compact.Header = "压缩";
            compact.Tag = bean.Name;
            compact.Click += new RoutedEventHandler(Compact_Click);
            cmenu.Items.Add(compact);

            MenuItem copy = new MenuItem();
            copy.Header = "复制";
            copy.Tag = bean.Name;
            copy.Click += new RoutedEventHandler(Copy_Click);
            cmenu.Items.Add(copy);

            MenuItem copyTo = new MenuItem();
            copyTo.Header = "复制到..";
            copyTo.Tag = bean.Name;
            copyTo.Click += new RoutedEventHandler(CopyTo_Click);
            cmenu.Items.Add(copyTo);

            Separator line = new Separator();
            cmenu.Items.Add(line);

            MenuItem removeAll = new MenuItem();
            removeAll.Header = "删除所有";
            removeAll.Tag = bean.Name;
            removeAll.Click += new RoutedEventHandler(RemoveAll_Click);
            cmenu.Items.Add(removeAll);

            MenuItem drop = new MenuItem();
            drop.Header = "Drop";
            drop.Tag = bean.Name;
            drop.Click += new RoutedEventHandler(Drop_Click);
            cmenu.Items.Add(drop);

            MenuItem stats = new MenuItem();
            stats.Header = "状态";
            stats.Tag = bean.Name;
            stats.Click += new RoutedEventHandler(Stats_Click);
            cmenu.Items.Add(stats);

            return cmenu;
        }

        private void Stats_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Drop_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void RemoveAll_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void CopyTo_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Copy_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Compact_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Index_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Insert_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Show_Top_1k(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(e.Source.ToString());
        }

        private void Show_Top_200(object sender, RoutedEventArgs e)
        {
            var item = e.Source as MenuItem;
            var collection = item.Tag as MongoCollection;
            var table = collection.FindAs<BsonDocument>(null).Skip(0).Take(100);
            mw.tabtext.Text = "";
            StringBuilder sb = new StringBuilder();
            int count = 1;
            foreach (var items in table)
            {
                sb.AppendLine("/* " + count + " */");
                sb.AppendLine(items.ToJson(StaticCs.JsonWriterSettings));
                count++;
            }
            mw.tabtext.Text = sb.ToString();

            var row = table.FirstOrDefault();
            if (null==row)
            {
                return;
            }
            var headernames = row.Names;
            DataTable dt = new DataTable();
            foreach (var column in headernames)
            {
                dt.Columns.Add(new DataColumn(column));
            }
            foreach (var items in table)
            {
                var nrow = dt.NewRow();
                foreach (var column in headernames)
                {
                    nrow[column] = items.GetValue(column).ToString();
                }
                dt.Rows.Add(nrow);
            }

            mw.dgtable.ItemsSource = dt.AsDataView();

            var tvlist = new List<TableChildItemModel>();
            var keys = table.FirstOrDefault().Names;
            foreach (var items in table)
            {
                var rlist = new TableChildItemModel();
                rlist.X = 999;
                foreach (var key in keys)
                {
                    var bean = new BodyTableTreeViewModel();
                    bean.Key = key;
                    //bean.Value = items.GetValue(key).ToString();
                    rlist.ChildItem.Add(bean);
                }
                tvlist.Add(rlist);
            }
            mw.tvdatatable.ItemsSource = tvlist;
        }

        private void Find_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(e.Source.ToString());
        }
    }
}
